package com.kexin.common.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kexin.common.exception.WebException;
import com.kexin.common.numeration.ExceptionEnum;
import com.kexin.common.user.entity.UserBase;
import com.kexin.common.user.mapper.UserBaseMapper;
import com.kexin.common.user.service.CouponFaceService;
import com.kexin.common.user.service.IUserBaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

/**
 * <p>
 * 用户基本信息表 服务实现类
 * </p>
 *
 * @author chen
 * @since 2021-01-27
 */
@Service
public class UserBaseServiceImpl extends ServiceImpl<UserBaseMapper, UserBase> implements IUserBaseService {

    @Autowired
    CouponFaceService couponFaceService;

    @Override
    public UserBase getCurrentUser() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Object currentUser = request.getSession().getAttribute("currentUser");
        if (currentUser == null) {
            throw new WebException(ExceptionEnum.ERROR_NOLOGIN);
        }
        return (UserBase)currentUser;
    }

    @Override
    public void saveOrUpdate(HttpServletRequest request,String token) {
        UserBase userBase = couponFaceService.getInfo(token);
        UserBase user = getOne(new LambdaQueryWrapper<UserBase>().eq(UserBase::getOpenId,userBase.getOpenId()));
        if (user == null){
            userBase.setCreateTime(LocalDateTime.now());
            save(userBase);
        }else {
            userBase.setId(user.getId());
            userBase.setCreateTime(user.getCreateTime());
            userBase.setUpdateTime(LocalDateTime.now());
            updateById(userBase);
        }
        request.getSession().setAttribute("isLogin",true);
        request.getSession().setAttribute("currentUser",userBase);
    }

    @Override
    public void checkLogin(HttpServletRequest request) {
        Object isLogin = request.getSession().getAttribute("isLogin");
        if (isLogin !=null){
            return;
        }
        String token = request.getHeader("X-Token");
        if (token == null){
            throw new WebException(ExceptionEnum.ERROR_NOLOGIN);
        }
        saveOrUpdate(request,token);
    }

    @Override
    public boolean isLogin() {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (requestAttributes == null){
            return false;
        }
        HttpServletRequest request = requestAttributes.getRequest();
        Object isLogin = request.getSession().getAttribute("isLogin");
        if (isLogin==null){
            return false;
        }
        return true;
    }
}
